package edu.hut.bookshop.service.impl;

import edu.hut.bookshop.dao.AftersaleMapper;
import edu.hut.bookshop.dao.OrderMapper;
import edu.hut.bookshop.pojo.Aftersale;
import edu.hut.bookshop.pojo.Order;
import edu.hut.bookshop.service.AftersaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AftersaleServiceImlp implements AftersaleService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AftersaleMapper aftersaleMapper;

    @Override
    public List<Aftersale> selectAftbyuserId(Integer userId) {
        //先通过用户id去查询所有的订单id
        List<Order> orders = orderMapper.selectByUserId(userId);
        System.out.println(orders);
        List<Aftersale> aftersales = new ArrayList<>();
        //再根据这些订单id去查询相关的售后表
        //判断支付状态，如果是未支付则不显示该售后表
        for(int i=0;i<orders.size();i++){
                if(aftersaleMapper.selectAfterbyorderId(orders.get(i).getOrderId()).getPstatus()!=0)
                    aftersales.add(aftersaleMapper.selectAfterbyorderId(orders.get(i).getOrderId()));
        }
        //返回集合
        return aftersales;
    }

    @Override
    public Aftersale selectAfterbyorderId(Integer orderId) {
        return aftersaleMapper.selectAfterbyorderId(orderId);
    }

    @Override
    public Integer updatebyorderId(Integer orderId,Integer pstatus) {
        return aftersaleMapper.updateAftersalebyorderId(orderId,pstatus);
    }

    @Override
    public Integer deletebyaftersaleId(Integer aftersaleId) {

        return aftersaleMapper.deletebyafterId(aftersaleId);
    }

    @Override
    public Order selectbyorderId(Integer orderId) {
        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public Integer updatejiajuname(Integer aftersaleId, String jiajuName) {
        return aftersaleMapper.updatejiajuName(aftersaleId,jiajuName);
    }

   /* @Override
    public Integer deletebyorderId(Integer orderId) {
        return aftersaleMapper.deletebyorderId(orderId);
    }*/
}
